package io.renren.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFHeader;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;

import io.renren.entity.Querypersonnelcommutertime;
import io.renren.entity.Shiftrangelist;
import io.renren.service.QuerypersonnelcommutertimeService;
import io.renren.service.ShiftrangelistService;
import io.renren.utils.DateUtils;
import io.renren.utils.PageUtils;
import io.renren.utils.R;

/**
 * 人员入场离场统计
 * 
 * @author zk
 * @email zk13899@163.com
 * @date 2017-11-06 10:56:45
 */
@Controller
@RequestMapping("querypersonnelcommutertime")
public class QuerypersonnelcommutertimeController {

	@Autowired
	private ShiftrangelistService shiftrangelistService;
	@Autowired
	private QuerypersonnelcommutertimeService querypersonnelcommutertimeService;

	@RequestMapping("/querypersonnelcommutertime.html")
	public String list() {
		return "querypersonnelcommutertime/querypersonnelcommutertime.html";
	}

	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	@RequiresPermissions("querypersonnelcommutertime:list")
	public R list(Integer page, Integer limit, 
			@RequestParam(value = "name") String name,
			@RequestParam(value = "type", defaultValue = "全部") String type, 
			@RequestParam(value = "time") String time) {
		Map<String, Object> map = new HashMap<>();
		map.put("offset_input", (page - 1) * limit);
		map.put("limit_input", limit);

		map.put("time", time);
		map.put("name", name);
		map.put("type", type);

		// 查询列表数据
		List<Querypersonnelcommutertime> querypersonnelcommutertimeList = querypersonnelcommutertimeService
				.queryList(map);
		int total = querypersonnelcommutertimeService.queryTotal(map);

		PageUtils pageUtil = new PageUtils(querypersonnelcommutertimeList, total, limit, page);

		return R.ok().put("page", pageUtil);
	}

	/* 区域列表 */
	@ResponseBody
	@RequestMapping("/class/list")
	public R arealist() {
		Map<String, Object> map = new HashMap<>();

		List<Shiftrangelist> shiftrangelist = shiftrangelistService.queryList(map);

		return R.ok().put("shiftrangelist", shiftrangelist);
	}

	/* 导出数据到excel */
	@RequestMapping("/excel")
	@SuppressWarnings({ "deprecation", "resource" })
	public String export1(HttpServletResponse response,
			@RequestParam(value = "type") String type,
			@RequestParam(value = "field") String field, 
			@RequestParam(value = "time") String time) throws Exception {
		Map<String, Object> map = new HashMap<>();
		map.put("type", type);
		map.put("field", field);
		map.put("time", time);

		// 如果是从数据库里面取数据 userList
		List<Querypersonnelcommutertime> userList = querypersonnelcommutertimeService.queryLists(map);// 用户集合

		System.out.println("++++++++++++++++++++++" + userList);
		/*
		 * 设置表头：对Excel每列取名(必须根据你取的数据编写)
		 */
		String[] tableHeader = { "姓名", "班次", "入场时间", "离岗时间", };
		/*
		 * 下面的都可以拷贝不用编写
		 */
		short cellNumber = (short) tableHeader.length;// 表的列数
		HSSFWorkbook workbook = new HSSFWorkbook(); // 创建一个excel
		HSSFCell cell = null; // Excel的列
		HSSFRow row = null; // Excel的行
		HSSFCellStyle style = workbook.createCellStyle(); // 设置表头的类型
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		HSSFCellStyle style1 = workbook.createCellStyle(); // 设置数据类型
		style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		HSSFFont font = workbook.createFont(); // 设置字体
		HSSFSheet sheet = workbook.createSheet("sheet1"); // 创建一个sheet
		HSSFHeader header = sheet.getHeader();// 设置sheet的头
		try {
			/**
			 * 根据是否取出数据，设置header信息
			 * 
			 */
			if (userList.size() < 1) {
				header.setCenter("查无资料");
			} else {
				System.out.println("导出表格的尺寸=" + userList);
				header.setCenter("公司表");
				row = sheet.createRow(0);
				row.setHeight((short) 400);
				for (int k = 0; k < cellNumber; k++) {
					cell = row.createCell(k);// 创建第0行第k列
					cell.setCellValue(tableHeader[k]);// 设置第0行第k列的值
					sheet.setColumnWidth(k, 5000);// 设置列的宽度
					font.setColor(HSSFFont.COLOR_NORMAL); // 设置单元格字体的颜色.
					font.setFontHeight((short) 200); // 设置单元字体高度
					style1.setFont(font);// 设置字体风格
					cell.setCellStyle(style1);
				}
				/*
				 * // 给excel填充数据这里需要编写
				 */
				for (int i = 0; i < userList.size(); i++) {
					Querypersonnelcommutertime user = (Querypersonnelcommutertime) userList.get(i);// 获取company对象
					row = sheet.createRow((short) (i + 1));// 创建第i+1行
					row.setHeight((short) 400);// 设置行高

					if (user.getNamePersonnel() != null) {
						cell = row.createCell(0);// 创建第i+1行第0列
						cell.setCellValue(user.getNamePersonnel());// 设置第i+1行第0列的值
						cell.setCellStyle(style);// 设置风格
					}
					if (user.getNameShiftrange() != null) {// 自己设置 的联系人不能为空
						cell = row.createCell(1); // 创建第i+1行第1列
						cell.setCellValue(user.getNameShiftrange());// 设置第i+1行第1列的值
						cell.setCellStyle(style); // 设置风格
					}
					// 由于下面的和上面的基本相同，就不加注释了

					if (user.getTriggertime() != null) {
						cell = row.createCell(2);
						cell.setCellValue(DateUtils.format(user.getTriggertime(), DateUtils.DATE_TIME_PATTERN));
						cell.setCellStyle(style);
					}

					if (user.getLasttime() != null) {
						cell = row.createCell(3);
						cell.setCellValue(DateUtils.format(user.getLasttime(), DateUtils.DATE_TIME_PATTERN));
						cell.setCellStyle(style);
					}

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		/*
		 * 下面的可以不用编写，直接拷贝
		 */

		OutputStream out = null;// 创建一个输出流对象
		try {

			out = response.getOutputStream();//
			response.setHeader("Content-disposition", "attachment; filename=" + "company.xls");// filename是下载的xls的名，建议最好用英文
			response.setContentType("application/msexcel;charset=UTF-8");// 设置类型
			response.setHeader("Pragma", "No-cache");// 设置头
			response.setHeader("Cache-Control", "no-cache");// 设置头
			response.setDateHeader("Expires", 0);// 设置日期头
			workbook.write(out);
			out.flush();
			workbook.write(out);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;

	}

}
